<?php
class safeService extends Service{
    private $table=null;
    
    public function __construct(){
        parent::__construct();
        $this->table=table('member');
    }
    //取得用户名，邮箱
    public function get_user_info(){
        $user_id=$_SESSION['MEM']['ID'];
        $query="select `id`,`user_name`,`email` from `{$this->table}` where `id`='{$user_id}' limit 1";
        return $this->m->select($query);
    }
    public function mod_password($d){
		$uc_user_service=new uc_user;
        $user_id=$_SESSION['MEM']['ID'];
        $result=array();
        $vali=validate::check('member',$d,'mod',array(),'member_pwd');
        if($vali){
		
			$username=$_SESSION['MEM']['USER_NAME'];
			$old_password=$d['old_user_password'];
			$password=$d['user_password'];
			$email=$d['email'];
			
			$ucresult = $uc_user_service->system_edit($username, $old_password, $password, $email);
			if($ucresult<=0){
				if($ucresult == -1) {
					alert('旧密码不正确！');
					back();
					
				} else if($ucresult == -4) {
					alert('Email 格式有误！');
					back();
					
				} else if($ucresult == -5) {
					alert('Email 不允许注册！');
					back();
					
				} else if($ucresult == -6) {
					alert('该 Email 已经被注册！');
					back();
					
				} else if($ucresult == 0){
					alert('没有做任何修改！');
					back();
					
				} else{
					alert('更新失败！');
					back();
					
				}
			}else{
				$fields='';
				
				if($email!=''){
					$fields.=" `email`='{$email}',";	
				}
				$fields=rtrim($fields,',');
				if($fields!=''){
					$query="update `{$this->table}` set {$fields} where `id`='{$user_id}'";
					$this->m->query($query);
				}
				$result[0]=true;
			}
			
            
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
    public function mod_email($d){
		$uc_user_service=new uc_user;
        $user_id=$_SESSION['MEM']['ID'];
        $result=array();
        $vali=validate::check('member',$d,'mod',array('id::!='=>$user_id),'member_email');
        if($vali){
			$username=$_SESSION['MEM']['USER_NAME'];
			$old_password=$d['old_user_password'];
			$password='';
			$email=$d['email'];
            
			$ucresult = $uc_user_service->system_edit($username, $old_password, $password, $email);
			if($ucresult<=0){
				if($ucresult == -1) {
					alert('旧密码不正确！');
					back();
					
				} else if($ucresult == -4) {
					alert('Email 格式有误！');
					back();
					
				} else if($ucresult == -5) {
					alert('Email 不允许注册！');
					back();
					
				} else if($ucresult == -6) {
					alert('该 Email 已经被注册！');
					back();
					
				} else if($ucresult == 0){
					alert('没有做任何修改！');
					back();
					
				} else{
					alert('更新失败！');
					back();
					
				}
			}else{
				$fields='';
				
				if($email!=''){
					$fields.=" `email`='{$email}',";	
				}
				$fields=rtrim($fields,',');
				if($fields!=''){
					$query="update `{$this->table}` set {$fields} where `id`='{$user_id}'";
					$this->m->query($query);
				}
				$result[0]=true;
			}
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
}